<?php

namespace App\Http\Controllers\Reception\Setting;

use App\Http\Controllers\Controller;
use App\Models\Setting;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Request;
// 设置模块
class SettingController extends Controller
{

    //获取站点信息
    public function getSetting(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $info = DB::table('setting_info')->get();
        if ($info) {
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }

    //获取首页状态
    public function getHomeCateStatus(Request $request){
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['status_id']=2;
        $info=DB::table('status')->where($map)->first();
        if($info){
            $data=success();
            $data['data']=$info;
        }else{
            $data=fail();
        }
        return $data;
    }

    //获取微信小程序广告
    public function getWxAds(Request $request){
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['setting_id']=1;
        $info=DB::table('setting')->where($map)->first();
        if($info){
            $info->value=json_decode($info->value);
            $data=success();
            foreach ($info->value as $key => $value) {
                if($key>1){
                    $data['data'][$key]=$value;
                }
            }
           $data['data']=array_values($data['data']);
        }else{
            $data=fail();
        }
        return $data;
    } 

    //获取QQ小程序广告
    public function getQQAds(Request $request){
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifytokens($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['setting_id']=5;
        $info=DB::table('setting')->where($map)->first();
        if($info){
            $info->value=json_decode($info->value);
            $data=success();
            foreach ($info->value as $key => $value) {
                if($key>1){
                    $data['data'][$key]=$value;
                }
            }
           $data['data']=array_values($data['data']);
        }else{
            $data=fail();
        }
        return $data;
    } 
    // public function getTest(){
        // $info=$_SERVER;
        // $info = Setting::where('sid', 1)
        //        ->orderBy('sid', 'desc')
        //        ->get();
        
        // $info=DB::table('setting_info')->where('sid', 1)->orderBy('sid', 'desc')->get();
        // return $info;
    // }
    // public function getTest()
    // {
    //     $map['setting_id'] = 1;
    //     $info = DB::table('setting')->where($map)->first();
    //     $info->value = json_decode($info->value, true);
    //     $Appid = $info->value[0]['value'];
    //     $AppSecret = Crypt::decryptString($info->value[1]['value']);
    //     $token=getWeiXinAccessToken($Appid,$AppSecret);
    //     // $data['res']=$res;
    //     // $data['cacheres']=Cache::get('wxAccessToken');
    //     // return $data;
    //     // $ip = get_real_ip();
    //     // $token=getWeiXinAccessToken();
    //     // $page='pages/data/data';
    //     // $scene="aid=16";
    //     // $url=getWeiXinUnlimited($scene, $page, $token);
    //     return $token;
    // }
    
}
